var conf = {
    serverHost: ""
};

var Hogan=require("hogan.js");

var _mm = {
    request: function (param) {
        var _this = this;
        $.ajax({
            type: param.method || 'get',
            url: param.url || '',
            dataType: param.type || 'json',
            data: param.data || '',
            success: function (res) {
                if (0 === res.status) {
                    typeof param.success === 'function' && param.success(res.data, res.msg);
                } else if (10 === res.status) {
                    _this.doLogin();
                } else if (1 === res.status) {
                    typeof param.error === 'function' && param.error(res.msg);
                }
            },
            error: function (err) {
                typeof param.error === 'function' && param.error(err.statusText);
            }
        });
    },
    getServerUrl: function (path) {
        return conf.serverHost + path;
    },
    //匹配对应的url参数
    getUrlParam: function (name) {
        var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
        var result = window.location.search.substr(1).match(reg);
        return result ? decodeURIComponent(result[2]) : null;
    },
    renderHtml:function(htmlTemplate,data){
        var template=Hogan.compile(htmlTemplate);
        result=template.render(data);
        return  result;
    },
    doLogin: function () {
        //告诉登录页是从哪个页面传过来的
        window.location.href = "./login.html?redirect=" + encodeURIComponent(window.location.href);
    },
    //成功提示
    successTip:function(msg){
        alert(msg||'操作成功');
    },
    //失败提示
    errorTip: function (msg) {
        alert(msg||'哪里不对了！')

    },
    validate:function(value,type){
        var value=$.trim(value);
        //非空验证
        if ('require' === type) {
            return !!value;
        }
        //手机验证
        if ('phone' === type) {
            return /^1\d{10}$/.test(value);
        }
        //邮箱格式验证
        if ('email' === type) {
            return /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(value);
        }
    },
    goHome:function(){
        window.location.href = './index.html';
    }
};

module.exports = _mm;